home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / JavaRadioStation / Source / com / apple / jens / radio / Radio.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  7.7 KB  |  204 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Radio.java
  3.     
  4.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  5.     
  6.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  7.                 ("Apple") in consideration of your agreement to the following terms, and your
  8.                 use, installation, modification or redistribution of this Apple software
  9.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  10.                 please do not use, install, modify or redistribute this Apple software.
  11.  
  12.                 In consideration of your agreement to abide by the following terms, and subject
  13.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  14.                 copyrights in this original Apple software (the "Apple Software"), to use,
  15.                 reproduce, modify and redistribute the Apple Software, with or without
  16.                 modifications, in source and/or binary forms; provided that if you redistribute
  17.                 the Apple Software in its entirety and without modifications, you must retain
  18.                 this notice and the following text and disclaimers in all such redistributions of
  19.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  20.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  21.                 Apple Software without specific prior written permission from Apple.  Except as
  22.                 expressly stated in this notice, no other rights or licenses, express or implied,
  23.                 are granted by Apple herein, including but not limited to any patent rights that
  24.                 may be infringed by your derivative works or by other works in which the Apple
  25.                 Software may be incorporated.
  26.  
  27.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  28.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  29.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  31.                 COMBINATION WITH YOUR PRODUCTS.
  32.  
  33.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  34.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  37.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  38.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  39.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.                 
  41.     Change History (most recent first):
  42.  
  43. */
  44.  
  45.  
  46. package com.apple.jens.radio;
  47.  
  48. import java.io.*;
  49. import java.net.*;
  50. import java.util.Date;
  51.  
  52. import com.apple.mrj.*;
  53.  
  54.  
  55. /** This is the main class of the streaming MP3 server.
  56.     It contains the "main" method that launches the server.
  57.     It also has static functions for console-based logging,
  58.     and stores the global default property values.
  59.     
  60. */
  61.     
  62. public class Radio {
  63.  
  64.     public static final String kStationsFolderName = "Stations";        //FIX: Needs to be localizable
  65.     
  66.     public static final String    kPropBufferSize = "buffer-size",
  67.                                 kPropLogLevel   = "log-level";
  68.     
  69.  
  70.     /** The "main" method that launches the server.
  71.         Command-line arguments are currently ignored.
  72.         Default property values are read from the "Defaults" file
  73.         in the current directory,
  74.         and station settings are read from files in the "Stations" directory. */
  75.     public static void main( String[] args ) {
  76.         System.out.println("*** "+getNameAndVersion()+" ***       Please excuse the cheesy console based UI...");
  77.         try{
  78.             RadioProperties.sDefaults = new RadioProperties(new File("Defaults"),null);
  79.         }catch( IOException x ) {
  80.             WARN(1,"Radio","Couldn't read default settings from 'Defaults' file:");
  81.             if( sLogLevel>=1 )
  82.                 x.printStackTrace(System.err);
  83.             return;
  84.         }
  85.         
  86.         sLogLevel = RadioProperties.sDefaults.getIntProperty(kPropLogLevel,sLogLevel);
  87.         
  88.         // Get buffer size from defaults:
  89.         int size = RadioProperties.sDefaults.getIntProperty(kPropBufferSize,0);
  90.         if( size >= 8000 )
  91.             Buffer.sSize = size;
  92.         
  93.         // Create a Station for each valid station file in the Stations directory:
  94.         File stationsDir = new File(kStationsFolderName);
  95.         if( !stationsDir.exists() || !stationsDir.isDirectory() ) {
  96.             WARN(1,"Radio","Couldn't find "+kStationsFolderName+" folder");
  97.             return;
  98.         }
  99.         String[] stationFiles = stationsDir.list();
  100.         
  101.         for( int i=0; i<stationFiles.length; i++ ) {
  102.             File stationFile = new File(stationsDir,stationFiles[i]);
  103.             if( isValidStationFile(stationFile) ) {
  104.                 LOG(2,"Radio", "Reading station file "+stationFile);
  105.                 try{
  106.                     RadioProperties stationProps = new RadioProperties(stationFile,RadioProperties.sDefaults);
  107.                     new Station(stationProps);    // Creates & starts a new Station thread
  108.                 }catch( IOException x ) {
  109.                     WARN(1,"Radio","Couldn't initialize station:");
  110.                     if( sLogLevel>=1 )
  111.                         x.printStackTrace(System.err);
  112.                 }
  113.             } else {
  114.                 WARN(2,"Radio", "Skipping \""+stationFile+"\" which isn't a valid station file"
  115.                                 +" (it's a directory, or invisible, or filetype isn't TEXT)");
  116.             }
  117.         }
  118.     }
  119.     
  120.     
  121.     /** Determines whether a file in the Stations directory is a valid station file.
  122.         Files whose names start with "." are skipped,
  123.         and on Mac OS so are files whose file-type is not 'TEXT'. */
  124.     private static boolean isValidStationFile( File f ) {
  125.         try{
  126.             if( f.isDirectory() )
  127.                 return false;
  128.             else if( f.getName().startsWith(".") )    // Really only applies to Unix...
  129.                 return false;
  130.             else if( MRJApplicationUtils.isMRJToolkitAvailable() &&
  131.                         ! MRJFileUtils.getFileType(f).equals(MRJOSType.kTypeTEXT) )
  132.                 return false;
  133.             else
  134.                 return true;
  135.             //! In Java2 should also check File.isHidden
  136.         }catch( IOException x ) {
  137.             x.printStackTrace();
  138.             return false;
  139.         }
  140.     }
  141.     
  142.     
  143.     // LOGGING / WARNINGS:
  144.     
  145.     
  146.     /** The global logging level; controls verbosity of console output.
  147.         Initialized from the kPropLogLevel property on startup.
  148.         <p>0: No output
  149.         <br>1: "Normal" output, mostly just track and connection info and important warnings.
  150.         <br>2: Adds information about buffers being filled and transmitted.
  151.         <br>3: Detailed synchronization info, for debugging purposes only. */
  152.     public static int sLogLevel = 1;
  153.     
  154.     
  155.     /** Returns the name and version number of the server software. */
  156.     public static String getNameAndVersion( ) {
  157.         return "Radio 0.3";    //FIX: Use real name
  158.     }
  159.     
  160.     
  161.     /** Writes a message to the log (currently the console at System.out.)
  162.         @param level      The level of message; sLogLevel has to be at least
  163.                         this high to print the message.
  164.         @param who        The object generating this message
  165.         @param message    The message to display. */
  166.     public static void LOG( int level, Object who, String message ) {
  167.         if( sLogLevel >= level ) {
  168.             timestamp(System.out, level, who+": "+message);
  169.             System.out.flush();
  170.         }
  171.     }
  172.     
  173.     
  174.     /** Writes a warning to the log (currently the console at System.err.)
  175.         @param level      The level of warning; sLogLevel has to be at least
  176.                         this high to print the message.
  177.         @param who        The object generating this warning
  178.         @param message    The warning message to display. */
  179.     public static void WARN( int level, Object who, String message ) {
  180.         if( sLogLevel >= level )
  181.             timestamp(System.err, level, "WARNING: "+who+": "+message);
  182.     }
  183.     
  184.  
  185.     /** Writes a time-stamp if it's been long enough since the last one was written. */
  186.     private static void timestamp( PrintStream s, int level, String msg ) {
  187.         long now = System.currentTimeMillis();
  188.         if( now - sLastStamp > kStampInterval ) {
  189.             s.println("____" + (new Date(now)) + "____");
  190.             sLastStamp = now;
  191.         }
  192.         s.print(level+"| ");
  193.         s.println(msg);
  194.     }
  195.     
  196.     
  197.     /** The time at which the last timestamp was written. */
  198.     private static long sLastStamp = 0;
  199.     
  200.     /** How often (in millis) timestamps are written to the log */
  201.     private static final long kStampInterval = 1 * 60 * 1000;    // 1 minute
  202.     
  203. }
  204.